#!/bin/sh -x

# remaster.sh - Copyright 2010 Brian Smith
# Licensed under GPLv2 License
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

alias grep="busybox grep"
alias sed="busybox sed"
alias awk="busybox awk"
alias wget="busybox wget"

cd_type=""

checkmountimage(){
	echo "cd location:  $cd_location"
	
	if [ -f $cd_location/boot/core.gz ]; then
		cd_type="cd"
	fi
	
	if [ -f $cd_location ]; then
		sudo busybox mount $cd_location $temp_dir/mnt || exit 16
		if [ -f $temp_dir/mnt/boot/core.gz ]; then
			cd_type="iso"	
		else 
			sudo umount $temp_dir/mnt
		fi
	fi	

	if [ ! -n "$cd_type" ]; then
		echo "Unable to find CD or mount ISO"
		exit 5;	
	fi
}

extractimage(){
	checkmountimage

	sudo rm -rf $temp_dir/extract
	sudo rm -rf $temp_dir/image
	sudo rm -rf $temp_dir/extract_apps
	sudo rm -rf $temp_dir/app_dep
	
	mkdir -p $temp_dir/app_dep
	mkdir -p $temp_dir/extract_apps
	mkdir -p $temp_dir/extract
	mkdir -p $temp_dir/image


	if [ $cd_type == "iso" ]; then
		sudo cp -a $temp_dir/mnt/* $temp_dir/image || exit 17
		sudo umount $temp_dir/mnt
	else
		sudo cp -a $cd_location/* $temp_dir/image || exit 18
	fi	

	cd $temp_dir/extract; zcat $temp_dir/image/boot/core.gz | sudo cpio -i -H newc -d >/dev/null 2>&1 || exit 19
}

bootcode(){
	if [ ! -f $temp_dir/image/boot/isolinux/isolinux.cfg ]; then
		echo "$temp_dir/image/boot/isolinux/isolinux.cfg not found"
		exit 9
	fi
	
	sudo busybox sed -i s/APPEND/append/ $temp_dir/image/boot/isolinux/isolinux.cfg
	
	for bootcode in `cat $input | grep "^cc = " | awk '{ $1=""; $2=""; print }'`; do
	        sudo busybox sed -i "/append/s|$| $bootcode|" $temp_dir/image/boot/isolinux/isolinux.cfg
	done
	
	if [ -n "`grep ^app_outside_initrd $input`" ]; then 
	        sudo busybox sed -i "/append/s|$| cde|" $temp_dir/image/boot/isolinux/isolinux.cfg
	fi
	
}

installapp(){
	app=$1
	location=$2

	getMirror	
	if echo $app | grep .scm$; then MIRROR="${MIRROR/tcz/scm}";fi
	
	if [ ! -f "$location"/"optional"/"$app" ]; then
	
		if [ -f /etc/sysconfig/tcedir/optional/"$app" ]; then
			sudo cp /etc/sysconfig/tcedir/optional/"$app" "$location"/"optional"/"$app"
			sudo cp /etc/sysconfig/tcedir/optional/"$app.md5.txt" "$location"/"optional"/"$app.md5.txt"
			sudo cp /etc/sysconfig/tcedir/optional/"$app.dep" "$location"/"optional"/"$app.dep"
		else	
			sudo wget -cq -O "$location"/"optional"/"$app" "$MIRROR"/"$app"	
			sudo wget -cq -O "$location"/"optional"/"$app.md5.txt" "$MIRROR"/"$app.md5.txt"	
			sudo wget -cq -O "$location"/"optional"/"$app.dep" "$MIRROR"/"$app.dep"
		fi

		if [ $location == $temp_dir/extract_apps/tce ]; then
			extract_tcz_script=`grep "^extract_tcz_script = " $input | awk '{print $3}'`
			
			sudo busybox mount $temp_dir/extract_apps/tce/optional/$app $temp_dir/mnt
			sudo busybox cp -a $temp_dir/mnt/* $temp_dir/extract
			sudo umount $temp_dir/mnt
			
			baseapp=`basename $app .tcz`
			if [ -x $temp_dir/extract/usr/local/tce.installed/$baseapp ]; then
				if [ $extract_tcz_script == "chroot" ]; then
					sudo chroot $temp_dir/extract ldconfig				
					sudo chroot $temp_dir/extract /usr/local/tce.installed/$baseapp					
					sudo umount $temp_dir/extract/proc >/dev/null 2>&1
				fi
				if [ $extract_tcz_script == "bootlocal" ]; then
					sudo echo "#### Added for $baseapp #####" >> $temp_dir/extract/opt/bootlocal.sh
					sudo cat $temp_dir/extract/usr/local/tce.installed/$baseapp | grep -v "^#!/bin/sh">> $temp_dir/extract/opt/bootlocal.sh
					sudo echo "#############################" >> $temp_dir/extract/opt/bootlocal.sh
				fi
			fi	
		fi
	fi
}

get_dependencies()
{
	app=${1//KERNEL/$kernel}
	location=$2
	
	getMirror	
	if echo $app | grep .scm$; then MIRROR="${MIRROR/tcz/scm}";fi

	if [ ! -f "$location"/"optional"/"$app" ]; then

		echo $app >> $temp_dir/app_dep/deplist.tmp

                if [ -f /etc/sysconfig/tcedir/optional/"$app" ]; then
                        cp /etc/sysconfig/tcedir/optional/"$app.dep" $temp_dir/app_dep/$app.dep
                else
                        wget -cq -O $temp_dir/app_dep/$app.dep "$MIRROR"/"$app.dep"
                fi

                if [ -f $temp_dir/app_dep/$app.dep ]; then
                        for depapp in `cat $temp_dir/app_dep/$app.dep`; do
                                get_dependencies $depapp $location
                        done
		fi
	fi
}

install(){
	app=$1
	location=$2

	rm $temp_dir/app_dep/deplist.tmp
	
	get_dependencies $app $location

	for depapp in `sed '1!G;h;$!d' "$temp_dir/app_dep/deplist.tmp"`; do
		installapp $depapp $location
	done

}

apps(){
	. /etc/init.d/tc-functions
	getMirror
	kernel=`uname -r`
	
	for app in `cat $input | grep "^app_outside_initrd_onboot = " | awk '{print $3}'`; do
		sudo mkdir -p $temp_dir/image/cde/optional
		if echo $app | grep -i .tcz$; then
			sudo sh -c "echo $app >> $temp_dir/image/cde/onboot.lst"
		fi
		if echo $app | grep -i .scm$; then
			sudo sh -c "echo ${app%.scm} >> $temp_dir/image/cde/scmboot.lst"
		fi
		install $app $temp_dir/image/cde
		if [ `grep "^copy2fs.flg$" $input ` ]; then sudo touch $temp_dir/image/cde/copy2fs.flg; fi
	done

	for app in `cat $input | grep "^app_outside_initrd = " | awk '{print $3}'`; do
		sudo mkdir -p $temp_dir/image/cde/optional
		install $app $temp_dir/image/cde
		if [ `grep "^copy2fs.flg$" $input ` ]; then sudo touch $temp_dir/image/cde/copy2fs.flg; fi
	done

	for app in `cat $input | grep "^app_inside_initrd_onboot = " | awk '{print $3}'`; do
		mkdir -p $temp_dir/extract/tmp/builtin/optional
		if echo $app | grep -i .tcz$; then
			sudo sh -c "echo $app >> $temp_dir/extract/tmp/builtin/onboot.lst"
		fi
		if echo $app | grep -i .scm$; then
			sudo sh -c "echo ${app%.scm} >> $temp_dir/extract/tmp/builtin/scmboot.lst"
		fi
		install $app $temp_dir/extract/tmp/builtin
	done

	for app in `cat $input | grep "^app_inside_initrd = " | awk '{print $3}'`; do
		mkdir -p $temp_dir/extract/tmp/builtin/optional
		install $app $temp_dir/extract/tmp/builtin
	done

	for app in `cat $input | grep "^app_extract_initrd = " | awk '{print $3}'`; do
		mkdir -p $temp_dir/extract_apps/tce/optional
		install $app $temp_dir/extract_apps/tce
	done

	mydata=`grep "^mydata = " $input | awk '{print $3}'`
	if [ -n "$mydata" ]; then
		sudo tar -xvzf $mydata -C $temp_dir/extract	
	fi
}

package(){
	sudo depmod -a -b $temp_dir/extract `uname -r`
	sudo ldconfig -r $temp_dir/extract
	
	cd $temp_dir/extract
	umount $temp_dir/extract/proc >/dev/null 2>&1
	
	sudo sh -c "find . | cpio -o -H newc | gzip -2 > $temp_dir/image/boot/core17.gz" || exit 22
	sudo advdef -z1 $temp_dir/image/boot/core17.gz || exit 23
	cdlabel="eCore"

	cd $temp_dir/image
	cat boot/isolinux/isolinux.cfg
	sudo mkisofs -l -J -R -V $cdlabel -no-emul-boot -boot-load-size 4 -boot-info-table -b boot/isolinux/isolinux.bin -c boot/isolinux/boot.cat -o $temp_dir/ezremaster.iso . || exit 24
}

if [ $# -ne 2 ]; then
	set +x
	echo ""
	echo "You must supply 2 arguments:  config-file and function"
	echo ""
	echo "Example:  remaster.sh /tmp/ezremaster/ezremaster.cfg rebuild"
	echo ""
	echo "Valid Functions are:"
	echo ""
	echo "   checkimage"
	echo "   extractimage"
	echo "   bootcode"
	echo "   apps"
	echo "   package"
	echo "   rebuild (does all of the above steps)"
	echo ""
	exit 1
fi

input=$1

if [ ! -r $input ]; then
	echo "Unable to open config file $input"
	exit 2
fi

function=$2

cd_location=`grep "^cd_location = " $input | awk '{print $3}'`
temp_dir=`grep "^temp_dir = " $input | awk '{print $3}'`

mkdir -p $temp_dir/image
mkdir -p $temp_dir/extract
mkdir -p $temp_dir/mnt
mkdir -p $temp_dir/app_dep

if [ ! -d $temp_dir/image ]; then
	echo "Error creating tmp dir:  $temp_dir/image"
	exit 7
fi

case $function in
	checkimage) 
		checkmountimage
		if [ $cd_type == "iso" ]; then
			sudo umount $temp_dir/mnt	
		fi
		;;
	extractimage) extractimage ;;
	bootcode) bootcode ;;
	apps) apps ;;
	package) package ;;
	rebuild)
		extractimage
		bootcode
		apps
		package
		;;
	*) echo "Invalid function: $function"; exit 3 ;;
esac

